package com.xiaobai;

import com.alibaba.nacos.shaded.io.grpc.LoadBalancer;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;

import java.util.List;

@SpringBootTest
public class OrderTestApplication {
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Test
    public void testServiceDiscovery(){
//        for (String service : discoveryClient.getServices()) {
//            System.out.println("服务名称:"+ service);
//        }
//        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
//        for (ServiceInstance instance : instances) {
//            System.out.println("host: " + instance.getHost() + "\t" + "port " + instance.getPort());
//        }
        ServiceInstance instance = loadBalancerClient.choose("service-product");
        System.out.println("choose: " + instance.getHost() + ": " + instance.getPort());

         instance = loadBalancerClient.choose("service-product");
        System.out.println("choose: " + instance.getHost() + ": " + instance.getPort());

         instance = loadBalancerClient.choose("service-product");
        System.out.println("choose: " + instance.getHost() + ": " + instance.getPort());

         instance = loadBalancerClient.choose("service-product");
        System.out.println("choose: " + instance.getHost() + ": " + instance.getPort());

    }
}
